<?php
/**
	+----------------------------------------------------------
	* ora Category 库文件
	* Author: lan7  E-mail: 7@lan7.net
	* @ora 扩展库文件 2010-10-10
	+----------------------------------------------------------
*/
class cat_lib{
	/**
	 * 获取下属类别
	 *
	 * @param int $Id
	 * @param int $deep
	 * @return Array
	 */
	public static function getSubChild($Id,$deep=3,$where=''){
		$rs = array();
		$query = $GLOBALS['db']->query("select * from ".tn('cat')." where pid = $Id $where order by sort desc");
		while (@$row = mysql_fetch_array($query,MYSQL_ASSOC)) {
			if(($deep-1)>0){
				$sub = self::getSubChild($row['Id'],$deep-1);
				if(!$sub){//没有子分类
					$rs[] = $row;
					continue;
				}else{
					if(($deep-2)>0){
						for ($j=0;$j<sizeof($sub);$j++){
							$sub[$j]['sub'] = self::getSubChild($sub[$j]['Id'],$deep-2);
						}
					}
					$row['sub'] = $sub;
				}
			}
			$rs[] = $row;
		}
		return $rs;
	}

	/**
	 * 获取某分类下所有子分类ID
	 *
	 * @param Integer $cat_id
	 * @return String
	 */
	public static function getCatIdStr($cat_id){
		$rs = array();
		$query = $GLOBALS['db']->query("select Id from ".tn('cat')." where pid = $cat_id  order by sort desc");
		while (@$row = mysql_fetch_array($query,MYSQL_ASSOC)) {
			$rs[] = $row['Id'];
		}
		$rs[] = $cat_id;
		return implode(",",$rs);
	}
}
?>